package com.easybenefit.UUService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UULoadPictureUtill;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUStrUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.activity.CouponActivity;
import com.easybenefit.UUService.activity.CouponsListActivity;
import com.easybenefit.UUService.activity.LoginActivity;
import com.easybenefit.UUService.activity.MainActivity;
import com.easybenefit.UUService.model.HttpUserBean;
import com.easybenefit.UUService.model.UserInfo;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;


public class Lancher extends Activity {

    private ImageView imageview_lancher;
    private String imageUrl = "";//launcher图片地址
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lancher);
        context = this;
//        imageview_lancher=(ImageView) this.findViewById(R.id.imageview_lancher);
//        UULoadPictureUtill loadPicture=new UULoadPictureUtill(Lancher.this,imageview_lancher);
//        loadPicture.getmImageLoader().get(imageUrl,loadPicture.getOne_listener());
//        new Thread() {
//            public void run() {
//                try {
//                    Thread.sleep(500);
//                } catch (Exception e) {
//                }
//                Message msg = new Message();
//                handler.sendMessage(msg);
//            }
//        }.start();
        final UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(Lancher.this, UUConfig.UU_USERINFO_NAME, UserInfo.class);
        if (userInfo == null) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }, 500);
        } else {
            login(userInfo.getUsername(), userInfo.getPassword());
        }
    }

//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            Intent intent = new Intent();
//            UserInfo userInfo= (UserInfo) UUSharedUtil.getObject(Lancher.this,UUConfig.UU_USERINFO_NAME,UserInfo.class);
//            if(userInfo==null){
//                intent.setClass(Lancher.this,LoginActivity.class);
//            }else{
//                intent.setClass(Lancher.this,MainActivity.class);
//            }
//            startActivity(intent);
//            Lancher.this.finish();
//        }
//    };

    /**
     * 登录
     *
     * @param userName
     * @param password
     */
    private void login(String userName, final String password) {
        if (!UUNetUtill.isConnected(context)) {
            UUToastUtil.showToast(context, UUDict.NOT_INTERNET);
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            String url = UUConfig.SERVER_BASE_URL + "merlogin";
            params.put("username", userName);
            params.put("password", password);
            client.setTimeout(5000);
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
//                    progressDialog = UUDialogUtill.getLoadingBar(LoginActivity.this, "正在登录....");
//                    progressDialog.show();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    try {
                        String content = new String(bytes);
                        HttpUserBean responseBean = new HttpUserBean();
                        responseBean = (HttpUserBean) UUJsonUtil.fromJson(content, HttpUserBean.class);
                        responseBean.getData().setPassword(password);
                        if (!UUConfig.SUCCESS_CODE.equals(responseBean.getCode())) {
                            UUToastUtil.showToast(context, responseBean.getMessage());
                            Intent intent = new Intent(context, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            UUSharedUtil.saveObject(context, UUConfig.UU_USERINFO_NAME, responseBean.getData());
                            Intent intent = new Intent(context, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Intent intent = new Intent(context, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        UUToastUtil.showToast(context, "登录错误重新登录");
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }

                @Override
                public void onFinish() {

                }
            });
        }
    }
}
